
/* 
MIT/X11 LICENSE
Copyright (C) 2012 Guy Pursey

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/


/*jslint browser: true, plusplus: true, devel: true*/

function Decimate(baseAlphabet) {
	"use strict";
	this.baseAlphabet = baseAlphabet;
	this.debugOn = false;
	var debugOn = false, helloworld = 'hello world. private var accessed',
		log = function (msg) {
			if (debugOn === true) {
				if (typeof console !== "undefined" && console.log) {
					console.log(msg);
				} else {
					alert(msg);
				}
			}
		};

	log('New Decimate object declared');
	log('Base alphabet: ' + this.baseAlphabet);
	log('Base alphabet length: ' + this.baseAlphabet.length);

	// TODO: need some kind of check on alphabet to ensure that characters are not duplicated -- maybe a setter function with an option to make the alphabet private?
	// TODO: also considering an option for reserve characters... this would be a true/false switch on the string provided I think

	this.debug = function (d) {
		if (typeof this.debugOn === "boolean") {
			this.debugOn = debugOn = d;
		}
		return 'Debug is currently set to ' + this.debugOn;
	};

	this.alphabet = function (baseAlphabet) {
		// can this check be optimised?

		var baseCheck = "",
			i = baseAlphabet.length - 1;
		do {
			if (baseCheck.search(baseAlphabet.charAt(i)) >= 0) {
				// duplicate found -- throw error?
			} else {
				baseCheck = baseAlphabet.charAt(i) + baseCheck;
			}
		} while (i--);

		this.baseAlphabet = baseAlphabet;
		return this.baseAlphabet;
	};

	this.convert = function (numToConvert) {

		// TODO: what if string received? would need a parseInt function before operations performed
		log('Number converting... ' + numToConvert);

		var numConverted = "", // a string to record the new numerals as the number is converted
			quotient = numToConvert,
			remainder = 0;

		do {
			numToConvert = quotient; // this is basically variable juggling!
			quotient = parseInt((numToConvert / this.baseAlphabet.length).toString().split(".")[0], 10);
			remainder = numToConvert - (quotient * this.baseAlphabet.length);
			numConverted = this.baseAlphabet.charAt(remainder) + numConverted; // write the number right-to-left
		} while (quotient >= this.baseAlphabet.length);

		numConverted = baseAlphabet.charAt(quotient.toString()) + numConverted;

		log('Number converted: ' + numConverted);
		return numConverted;

	};

	this.revert = function (numStrToRevert) {
		log('Number reverting... ' + numStrToRevert);
		var numReverted = 0,
			i = 0;

		do {
			numReverted += this.baseAlphabet.indexOf(numStrToRevert.charAt(i));
			numReverted = numReverted * this.baseAlphabet.length;
			i++;
		} while (i < numStrToRevert.length - 1);
		numReverted += this.baseAlphabet.indexOf(numStrToRevert.charAt(i));

		log('Number reverted: ' + numReverted);
		return numReverted;
	};
}